package com.project.service.impl;
import com.project.bean.RoomBean;
import com.project.mapper.IRoomMapper;
import com.project.mapper.IUnitMapper;
import com.project.service.IRoomService;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class RoomServiceImpl  implements IRoomService {

    @Autowired
    private IRoomMapper mapper;

    @Autowired
    private IUnitMapper unitMapper;

    @Override
    public List<RoomBean> findByItem(String address, String state,
                                     String bool, String type) {

        return mapper.findName(address,state,bool,type);
    }

    @Override
    public void add(RoomBean room) {

        mapper.add(room);
    }

    @Override
    public boolean del(Integer roomId) {
        if (mapper.countStuByRoom(roomId) == 0){

            unitMapper.delAll(roomId);
            mapper.del(roomId);
            return true;
        }
        return false;
    }


    @Override
    public RoomBean findById(Integer id) {
        RoomBean roomBean = mapper.findById(id);
        roomBean.setNum(mapper.countStuByRoom(id));
        return roomBean;
    }

}
